#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "Shader.hpp"
#include "MeshRenderer.hpp"
#include "Texture.hpp"
#include <cmath>

void frame_buff_size_callback(GLFWwindow* window, int width, int height);
void pre_render();
void render();


int main() {

    // GLFW INIT
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "Hello World", nullptr, nullptr);
    if(window == nullptr) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwSetFramebufferSizeCallback(window, frame_buff_size_callback);
    glfwMakeContextCurrent(window);

    // GLAD
    if(!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    glViewport(0, 0, 800, 600);

    pre_render();

    while (!glfwWindowShouldClose(window)) {
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

void frame_buff_size_callback(GLFWwindow* window, const int width, const int height) {
    glViewport(0, 0, width, height);
}

Shader* shader;
MeshRenderer* quad_mesh_renderer;
Texture* texture1;
Texture* texture2;

void pre_render() {
    shader = new Shader("Assets/Shader/05_Texture/shader2.vert", "Assets/Shader/05_Texture/shader2.frag");
    texture1 = new Texture("Assets/Texture/t1.png");
    texture2 = new Texture("Assets/Texture/t2.png");
    {
        float vertices[] = {
            //     ---- 位置 ----       ---- 颜色 ----     - 纹理坐标 -
            0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // 右上
            0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // 右下
           -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // 左下
           -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // 左上
        };
        unsigned int indices[] = {
            0, 1, 3, // 第一个三角形
            1, 2, 3  // 第二个三角形
        };
        quad_mesh_renderer = new MeshRenderer(MeshRenderer::vertex_format_position | MeshRenderer::vertex_format_color | MeshRenderer::vertex_format_uv1);
        quad_mesh_renderer->SetShader(shader);
        quad_mesh_renderer->SetVertex(vertices, std::size(vertices));
        quad_mesh_renderer->SetIndices(indices, std::size(indices));
        quad_mesh_renderer->Init();
    }
}

void render() {
    quad_mesh_renderer->PreDraw();
    double timeValue = glfwGetTime();
    float greenValue = static_cast<float>(sin(timeValue)) / 2.0f + 0.5f;
    quad_mesh_renderer->GetShader()->SetVector("outColor", 0, greenValue, 0, 1);
    texture1->Use(GL_TEXTURE0);
    texture2->Use(GL_TEXTURE1);
    quad_mesh_renderer->GetShader()->SetInt("ourTexture1", 0);
    quad_mesh_renderer->GetShader()->SetInt("ourTexture2", 1);
    quad_mesh_renderer->Draw();
}